package com.zjhn.ds.utils;

import com.alibaba.fastjson.JSON;
import com.zjhn.ds.common.AuthenticationUser;
import com.zjhn.ds.common.Constant;
import com.zjhn.ds.domain.entity.erp.Department;
import com.zjhn.ds.domain.entity.erp.Operator;
import com.zjhn.ds.domain.vo.EquipmentVo;
import com.zjhn.ds.domain.vo.ModelVo;
import com.zjhn.ds.domain.vo.OperatorVo;
import com.zjhn.ds.domain.vo.StationVo;
import com.zjhn.ds.mapper.*;
import com.zjhn.ds.utils.base.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @BelongsProject: ds_jlw
 * @BelongsPackage: com.zjhn.ds.utils
 * @Author: sxt
 * @CreateTime: 2023-09-07  16:42
 * @Description:
 * @Version: 1.0
 */
@Slf4j
@Component
public class CommonUtil {

    @Resource
    private RedisCache redisCache;

    @Resource
    private OperatorMapper operatorMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    // --------------------------------------------------------------------------------------------------------------

    /**
     * 获取单个机台当前状态 1-在线/运行, 2-待机/离线, 3-关机/掉线
     * @return
     */
    public Integer getStationStatus(String stationId) {
        Integer res = null;
        if (StringUtils.isNotBlank(stationId)) {
            Object object = redisCache.getCacheObject(Constant.REDIS_PRE.STATION_STATUS + stationId);
            if (object == null)
                res = 3;
            else
                res = Integer.getInteger(String.valueOf(object));
        }
        return res;
    }


    // --------------------------------------------------------------------------------------------------------------


    /**
     * 缓存中获取员工信息
     *
     * @param operatorId
     * @return
     */
    public OperatorVo getOperatorById(String operatorId) {
        OperatorVo res = OperatorVo.builder().build();
        try {
            if (StringUtils.isNotBlank(operatorId)) {
                Object object = redisCache.getCacheObject(Constant.REDIS_PRE.OPERATE + operatorId);
                if (object == null) {
                    // 重新缓存
                    Operator operator = operatorMapper.selectById(operatorId);
                    if (operator != null && operator.getIsDeleted() == 0) {
                        res.setId(operator.getId());
                        res.setUsername(operator.getUsername());
                        res.setRealName(operator.getRealName());
                        res.setUserNo(operator.getUserNo());
                        res.setDepartmentGuid(operator.getDepartmentGuid());
                        // 缓存数据
                        redisCache.setCacheObject(Constant.REDIS_PRE.OPERATE + operator.getId(),
                                JSON.toJSONString(res));
                    }
                } else
                    res = JSON.parseObject(String.valueOf(object), OperatorVo.class);
            }
        } catch (Exception e) {
            log.error("====[ERROR]========CommonUtil====getOperatorById 缓存中获取员工信息异常");
        }
        return res;
    }

    /**
     * 缓存中获取工位信息
     *
     * @param stationId
     * @return
     */
    public StationVo getStationById(String stationId) {
        StationVo res = StationVo.builder().build();
        try {
            if (StringUtils.isNotBlank(stationId)) {
                Object object = redisCache.getCacheObject(Constant.REDIS_PRE.STATION + stationId);
                if (object != null)
                    res = JSON.parseObject(String.valueOf(object), StationVo.class);
            }
        } catch (Exception e) {
            log.error("====[ERROR]========CommonUtil====getStationById 缓存中获取工位信息异常");
        }
        return res;
    }

    /**
     * 缓存中获取设备类别
     *
     * @param equipmentTypeId
     * @return
     */
    public String getEquipmentTypeById(String equipmentTypeId) {
        try {
            if (StringUtils.isNotBlank(equipmentTypeId)) {
                Object object = redisCache.getCacheObject(Constant.REDIS_PRE.EQUIPMENT_TYPE + equipmentTypeId);
                if (object != null)
                    return String.valueOf(object);
            }
        } catch (Exception e) {
            log.error("====[ERROR]========CommonUtil====getEquipmentTypeById 缓存中获取设备类别异常");
        }
        return null;
    }

    /**
     * 缓存中获取模型信息
     *
     * @param modelId
     * @return
     */
    public ModelVo getModelById(String modelId) {
        ModelVo res = ModelVo.builder().build();
        try {
            if (StringUtils.isNotBlank(modelId)) {
                Object object = redisCache.getCacheObject(Constant.REDIS_PRE.MODEL + modelId);
                if (object != null)
                    res = JSON.parseObject(String.valueOf(object), ModelVo.class);
            }
        } catch (Exception e) {
            log.error("====[ERROR]========CommonUtil====getStationById 缓存中获取模型信息异常");
        }
        return res;
    }

    /**
     * 缓存中获取车间信息
     *
     * @param workshopId
     * @return
     */
    public String getWorkshopById(String workshopId) {
        try {
            if (StringUtils.isNotBlank(workshopId)) {
                Object object = redisCache.getCacheObject(Constant.REDIS_PRE.WORKSHOP + workshopId);
                if (object != null)
                    return String.valueOf(object);
            }
        } catch (Exception e) {
            log.error("====[ERROR]========CommonUtil====getWorkshopById 缓存中获取车间信息异常");
        }
        return null;
    }

    /**
     * 缓存中获取部门信息
     *
     * @param departmentId
     * @return
     */
    public String getDepartmentId(String departmentId) {
        try {
            if (StringUtils.isNotBlank(departmentId)) {
                Object object = redisCache.getCacheObject(Constant.REDIS_PRE.DEPARTMENT + departmentId);
                if (object != null)
                    return String.valueOf(object);
                else {
                    Department department = departmentMapper.selectById(departmentId);
                    if (department != null) {
                        redisCache.setCacheObject(Constant.REDIS_PRE.DEPARTMENT + departmentId, department.getTitle());
                        return department.getTitle();
                    }
                }
            }
        } catch (Exception e) {
            log.error("====[ERROR]========CommonUtil====getWorkshopById 缓存中获取车间信息异常");
        }
        return null;
    }

    /**
     * 缓存中获取设备信息
     *
     * @param equipmentId
     * @return
     */
    public EquipmentVo getEquipmentById(String equipmentId) {
        EquipmentVo res = EquipmentVo.builder().build();
        try {
            if (StringUtils.isNotBlank(equipmentId)) {
                Object object = redisCache.getCacheObject(Constant.REDIS_PRE.EQUIPMENT + equipmentId);
                if (object != null)
                    res = JSON.parseObject(String.valueOf(object), EquipmentVo.class);
            }
        } catch (Exception e) {
            log.error("====[ERROR]========CommonUtil====getStationById 缓存中获取设备信息");
        }
        return res;
    }

    /**
     * 缓存中获取工位绑定信息
     *
     * @param stationId
     * @return
     */
    public String getBindInfoById(String stationId) {
        try {
            if (StringUtils.isNotBlank(stationId)) {
                Object object = redisCache.getCacheObject(Constant.REDIS_PRE.BIND_STATION_EQUIPMENT + stationId);
                if (object != null)
                    return String.valueOf(object);
            }
        } catch (Exception e) {
            log.error("====[ERROR]========CommonUtil====getStationById 缓存中获取设备信息");
        }
        return null;
    }

    // --------------------------------------------------------------------------------------------------------------


    /**
     * 新增/更新车间缓存信息
     *
     * @param workshopId
     * @param workshopName
     */
    public void updateWorkshopInfo(String workshopId, String workshopName) {
        redisCache.setCacheObject(Constant.REDIS_PRE.WORKSHOP + workshopId, workshopName);
    }

    /**
     * 删除车间缓存信息
     *
     * @param workshopId
     */
    public void removeWorkshopInfo(String workshopId) {
        redisCache.deleteObject(Constant.REDIS_PRE.WORKSHOP + workshopId);
    }

    /**
     * 新增/更新模型数据
     *
     * @param modelId
     * @param modelVo
     */
    public void updateModelInfo(String modelId, ModelVo modelVo) {
        redisCache.setCacheObject(Constant.REDIS_PRE.MODEL + modelId, JSON.toJSONString(modelVo));
    }

    /**
     * 删除模型缓存数据
     *
     * @param modelId
     */
    public void removeModelInfo(String modelId) {
        redisCache.deleteObject(Constant.REDIS_PRE.MODEL + modelId);
    }

    /**
     * 新增/更新设备类别
     *
     * @param equipmentTypeId
     * @param equipmentTypeName
     */
    public void updateEquipmentType(String equipmentTypeId, String equipmentTypeName) {
        redisCache.setCacheObject(Constant.REDIS_PRE.EQUIPMENT_TYPE + equipmentTypeId, equipmentTypeName);
    }

    /**
     * 删除设备类别缓存信息
     *
     * @param equipmentTypeId
     */
    public void removeEquipmentType(String equipmentTypeId) {
        redisCache.deleteObject(Constant.REDIS_PRE.EQUIPMENT_TYPE + equipmentTypeId);
    }

    /**
     * 新增/更新工位信息
     *
     * @param stationId
     * @param stationVo
     */
    public void updateStationInfo(String stationId, StationVo stationVo) {
        redisCache.setCacheObject(Constant.REDIS_PRE.STATION + stationId, JSON.toJSONString(stationVo));
    }

    /**
     * 删除工位缓存信息
     *
     * @param stationId
     */
    public void removeStationInfo(String stationId) {
        redisCache.deleteObject(Constant.REDIS_PRE.STATION + stationId);
    }

    /**
     * 新增/更新设备信息
     *
     * @param equipmentId
     * @param equipmentVo
     */
    public void updateEquipmentInfo(String equipmentId, EquipmentVo equipmentVo) {
        redisCache.setCacheObject(Constant.REDIS_PRE.EQUIPMENT + equipmentId, JSON.toJSONString(equipmentVo));
    }

    /**
     * 删除设备缓存信息
     *
     * @param equipmentId
     */
    public void removeEquipmentInfo(String equipmentId) {
        redisCache.deleteObject(Constant.REDIS_PRE.EQUIPMENT + equipmentId);
    }

    /**
     * 新增/更新工位绑定信息
     *
     * @param stationId
     * @param equipmentId
     */
    public void updateBindInfo(String stationId, String equipmentId) {
        redisCache.setCacheObject(Constant.REDIS_PRE.BIND_STATION_EQUIPMENT + stationId, equipmentId);
    }

    /**
     * 删除设备类别缓存信息
     *
     * @param stationId
     */
    public void removeBindInfo(String stationId) {
        redisCache.deleteObject(Constant.REDIS_PRE.BIND_STATION_EQUIPMENT + stationId);
    }
}
